from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os
import database

class Home(webapp.RequestHandler):
	def get(self):
		path = os.path.join(os.path.dirname(__file__), 'templates/admin.html')
		self.response.out.write(template.render(path, {
			}))
			
class CommonRoom(webapp.RequestHandler):
	def post(self):
		self.response.headers['Content-Type'] = "text/plain; charset=utf-8"
		action = self.request.get('action')
		key = self.request.get('key')
		if action=='create':
			database.CommonRoom(key_name=key, name=key, description=self.request.get('description')).put()
			self.response.out.write('Room '+ key +" created")

class StudentRoom(webapp.RequestHandler):
	def post(self):
		self.response.headers['Content-Type'] = "text/plain; charset=utf-8"
		action = self.request.get('action')
		key = self.request.get('key')
		if action=='create':
			database.StudentRoom(key_name=key, number=int(key)).put()
			self.response.out.write('Room '+ key +" created")

class Year(webapp.RequestHandler):
	def post(self):
		self.response.headers['Content-Type'] = "text/plain; charset=utf-8"
		action = self.request.get('action')
		key = self.request.get('key')
		if action=='create':
			import datetime
			startdate = datetime.date(day=1, month=10, year=2003+int(key))
			enddate = datetime.date(day=30, month=9, year=2004+int(key))
			database.Year(key_name=key, number=int(key), startdate=startdate, enddate=enddate).put()
			self.response.out.write('Year '+ key +" created")

class Person(webapp.RequestHandler):
	def post(self):
		self.response.headers['Content-Type'] = "text/plain; charset=utf-8"
		action = self.request.get('action')
		nick = self.request.get('nick')
		name = self.request.get('name')
		surname = self.request.get('surname')
		persontype = self.request.get('type')
		if action=='create':
			if persontype=='student':
				database.Student(key_name=nick, nick=nick, name=name, surname=surname).put()
				self.response.out.write('Student '+ surname+ ' ' + name +" created")

application = webapp.WSGIApplication([
		('/admin/', Home),
		('/admin/commonroom', CommonRoom),
		('/admin/studentroom', StudentRoom),
		('/admin/year', Year),
		('/admin/person', Person),
	],
	debug=True
)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
